/*
 * SpectraSTTrainModelParams.cpp
 *
 *  Created on: 28 Jul, 2012
 *      Author: yhu
 */
#include "SpectraSTTrainModelParams.hpp"
using namespace std;

SpectraSTTrainModelParams::SpectraSTTrainModelParams() {
	setDefault();
}

SpectraSTTrainModelParams::~SpectraSTTrainModelParams() {

}

void SpectraSTTrainModelParams::setDefault() {
	m_maxRoundsNum = 10000;
	m_predictPeaksNum = 30;
	m_fragTolerance = 0.5;
	m_modelName = "RBM";
	m_outputFileName = "new_model.txt";
}

void SpectraSTTrainModelParams::printAdvancedOptions(ostream& out) {
	out << "SpectraST -t_ (advanced options of training model)" << endl;
}

void SpectraSTTrainModelParams::printUsage(ostream& out) {
	out << "SpectraSTTrainModelParams::printUsage" << endl;
}

bool SpectraSTTrainModelParams::addOption(string option) {
	//cout << "SpectraSTTrainModelParams::add option" << option << endl;

	if (option.length() == 0) {
		return true;
	} else {
		if (isExpectingArg(option)) {
			return (false);
		} else {
			m_options.push_back(option);
			return (true);
		}
	}
}

void SpectraSTTrainModelParams::finalizeOptions() {
	cout << "SpectraSTTrainModelParams::finalizeOptions" << endl;
	vector<string>::iterator i;
	for (i = m_options.begin(); i != m_options.end(); i++) {
		bool valid = false;
		string extension;

		char optionType = (*i)[0];
		string optionValue = (*i).length() > 1 ? (*i).substr(1) : "";

		switch (optionType) {
		case 'm':
			if (optionValue.empty()) {
				m_modelName = "RBM";
			} else {
				m_modelName = optionValue;
			}
			valid = true;
			break;
		case 'o':
			if (optionValue.empty()) {
				m_outputFileName = "new_model.txt";
			} else {
				m_outputFileName = optionValue;
			}
			valid = true;
			break;
		case '_':
			finalizeAdvancedOption(optionValue);
			valid = true;
			break;
		}

		if (!valid) {
			cout << "option -t" << optionType << "is invalid and ignored!"
					<< endl;
		}
	}
	//debug
	if (1) {
		printParams();
	}
}

void SpectraSTTrainModelParams::finalizeAdvancedOption(string option) {
	//cout << "SpectraSTTrainModelParams::finalizeAdvancedOption" << option << endl;

	double f = -1.0;
	int k = 0;

	if (option.length() < 3) {
		cout << "Advanced option \"-c_" << option
				<< "\" is undefined. Ignored.	" << endl;
		return;
	}

	bool valid = false;

	string optionType = option.substr(0, 3);
	string optionValue = option.substr(3);

	if (optionType == "MRN") {
		if (!optionValue.empty()) {
			k = atoi(optionValue.c_str());
			if (k >= 0) {
				m_maxRoundsNum = k;
				valid = true;
			}
		}
	} else if (optionType == "PPN") {
		if (!optionValue.empty()) {
			k = atoi(optionValue.c_str());
			if (k >= 0) {
				m_predictPeaksNum = k;
				valid = true;
			}
		}
	}

	if (!valid) {
		cout << "Advanced option -t " << option << "is undefined and ignored!"
				<< endl;
	}
}

bool SpectraSTTrainModelParams::isExpectingArg(string option) {
	if ((option == "m") || (option == "o")) {
		return (true);
	}
	return (false);
}

void SpectraSTTrainModelParams::printParams() {
	cout << "print all SpectraSTTrainModelParams:" << endl;
	cout << "m_maxRoundsNum = " << m_maxRoundsNum << endl;
	cout << "m_predictPeaksNum = " << m_predictPeaksNum << endl;
	cout << "m_fragTolerance = " << m_fragTolerance << endl;
	cout << "m_modelName = " << m_modelName << endl;
	cout << "m_outputFileName = " << m_outputFileName << endl;
}
